package com.cyy.dyeing.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cyy.dyeing.common.DataStatusEnum;
import com.cyy.dyeing.common.Result;
import com.cyy.dyeing.dto.InfoQueryDto;
import com.cyy.dyeing.exception.BusinessException;
import com.cyy.dyeing.exception.SystemException;
import com.cyy.dyeing.model.Info;
import com.cyy.dyeing.service.InfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.SyncFailedException;
import java.util.UUID;

/**
 * @Author: cyy
 * @Date: 2024/7/25
 */
@RestController
@RequestMapping("/info")
public class InfoController {
    @Autowired
    private InfoService infoService;
    /**
     * 信息列表
     * @param infoDto
     * @return
     */
    @GetMapping
    public Result getPage(InfoQueryDto infoDto){
        Page<Info> pageInfo = new Page<>(infoDto.getPageNum(), infoDto.getPageSize());
        LambdaQueryWrapper<Info> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(infoDto.getInfo_type()!=null, Info::getInfo_type, infoDto.getInfo_type())
                .like(infoDto.getInfo_name()!=null, Info::getInfo_name, infoDto.getInfo_name())
                .like(infoDto.getInfo_remark()!=null, Info::getInfo_remark, infoDto.getInfo_remark())
                .eq(infoDto.getInfo_status()!=null, Info::getInfo_status, infoDto.getInfo_status());
        Page<Info> page = infoService.page(pageInfo, queryWrapper);
        return Result.ok(page);
    }

    /**
     * 添加信息
     * @param info
     * @return
     */
    @PostMapping
    public Result addInfo(@RequestBody Info info){
        try {
            info.setInfo_id(UUID.randomUUID().toString().replace("-", ""));
            info.setInfo_status(DataStatusEnum.UNDETERMINED.getCode());

        }catch (Exception e){
            throw new SystemException("系统出错");
        }
        infoService.save(info);
        return Result.ok();
    }

    /**
     * 根据编号删除一个
     * @param info_type
     * @param eid
     * @return
     */
    @DeleteMapping("/{info_type}/{eid}")
    public Result infoDel(@PathVariable int info_type, @PathVariable String eid){
        infoService.deleteInfo(info_type, eid);
        return Result.ok();
    }

    /**
     * 批量删除
     * @param info_type
     * @param ids
     * @return
     */
    @DeleteMapping("/{info_type}")
    public Result infoDel(@PathVariable int info_type, @RequestBody String... ids){
        if(ids == null || ids.length == 0){
            throw new BusinessException("没有可删除的信息");
        }
        for (String id :ids) {
            infoService.deleteInfo(info_type, id);
        }
        return Result.ok();
    }

    /**
     * 状态操作
     * @param info_type
     * @param info_id
     * @param info_status
     * @return
     */
    @PutMapping("/{info_type}/{info_id}/{info_status}")
    public Result infoChangeStatus(@PathVariable("info_type") int info_type,
                                   @PathVariable("info_id") String info_id,
                                   @PathVariable("info_status") String info_status){
        String msg = infoService.infoChangeStatus(info_type, info_id, info_status);
        return Result.ok(msg);
    }
}
